package com.chunxuan.langquan.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chunxuan.langquan.R;
import com.chunxuan.langquan.base.Base2Activity;
import com.chunxuan.langquan.base.BaseResult2;
import com.chunxuan.langquan.base.Global;
import com.chunxuan.langquan.dao.bean.CourseDetail;
import com.chunxuan.langquan.dao.bean.PageBean;
import com.chunxuan.langquan.dao.http.APIRetrofit2;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class CourseTeacherCommentAllActivity extends Base2Activity {
    private ImageView iv_back;
    private RecyclerView rcv_labels;// 学员评价标签
    private RecyclerView rcv_comment_list;// 学员评价列表

    private String course_id;// 传过来的课程id;
    private ArrayList<String> labelList = new ArrayList<>();// 传过来的评论标签列表

    private LabeListAdapter labelAdapter;

    protected List<CourseDetail.CommentInfo> commentInfoList = new ArrayList<>();
    private CommentListAdapter commentListAdapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_course_teacher_comment_all;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iv_back = findViewById(R.id.iv_back);
        rcv_labels = findViewById(R.id.rcv_labels);
        rcv_comment_list = findViewById(R.id.rcv_comment_list);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            course_id = bundle.getString("course_id");
            labelList = (ArrayList<String>) bundle.getSerializable("labelList");
        }

        labelAdapter = new LabeListAdapter(this);
        GridLayoutManager manager = new GridLayoutManager(this, 4);
        rcv_labels.setLayoutManager(manager);
        rcv_labels.setAdapter(labelAdapter);
        labelAdapter.setDataList(labelList);

        setOnClickListener(iv_back);

        reqCommentInfo();
        showProgress();
    }

    private Disposable reqCommentInfo() {
        return APIRetrofit2.getDefault().reqCommentList(Global.HEADER, course_id, "1", "999999")
                .compose(RxUtil.<BaseResult2<PageBean<CourseDetail.CommentInfo>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<PageBean<CourseDetail.CommentInfo>>>() {
                    @Override
                    public void accept(BaseResult2<PageBean<CourseDetail.CommentInfo>> result2) throws Exception {
                        hideProgress();
                        if (result2.isSuccess()) {
                            PageBean page = result2.getData();
                            if (null != page) {
                                commentInfoList = page.getData();
                                LinearLayoutManager manager = new LinearLayoutManager(CourseTeacherCommentAllActivity.this);
                                rcv_comment_list.setLayoutManager(manager);
                                commentListAdapter = new CommentListAdapter(CourseTeacherCommentAllActivity.this);
                                rcv_comment_list.setAdapter(commentListAdapter);
                                commentListAdapter.setDataList(commentInfoList);
                            } else {

                            }
                        } else {
                            ToastUtils.showShort(result2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("获取课程详情失败");
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == iv_back) {
            onBackPressed();
        }
    }

    private class LabeListAdapter extends ListBaseAdapter<String> {

        public LabeListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_teacher_info_label;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_label = holder.getView(R.id.tv_label);
            tv_label.setText(mDataList.get(position));
        }
    }

    private class CommentListAdapter extends ListBaseAdapter<CourseDetail.CommentInfo> {

        public CommentListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_rlv_course_teacher_comment_all;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_time = holder.getView(R.id.tv_time);
            TextView tv_pingjia_custom = holder.getView(R.id.tv_pingjia_custom);
            RecyclerView rcv_labels = holder.getView(R.id.rcv_labels);

            CourseDetail.CommentInfo info = mDataList.get(position);
            tv_name.setText(info.getUser_name());
            tv_time.setText(info.getComment_time());
            tv_pingjia_custom.setText(info.getContent());
            //全部评价-是否展示评价内容
//            tv_pingjia_custom.setVisibility(View.INVISIBLE);

            GridLayoutManager manager = new GridLayoutManager(CourseTeacherCommentAllActivity.this, 4);
            rcv_labels.setLayoutManager(manager);
            LabeListAdapter adapter = new LabeListAdapter(CourseTeacherCommentAllActivity.this);
            rcv_labels.setAdapter(adapter);

            try {
                String labels = info.getTags();
                if (!TextUtils.isEmpty(labels)) {
                    String[] labelArr = labels.split(",");
                    List<String> labelList = Arrays.asList(labelArr);
                    adapter.setDataList(labelList);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}